package com.CoreryBlack.controller;

import com.CoreryBlack.po.Result;
import com.CoreryBlack.properties.AliyunOSSProperties;
import com.CoreryBlack.util.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description UploadController
 * @Author CoreryBlack
 * @Date 2024-09-20
 */
@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;
    /**
     * 文件上传
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("文件上传: {}", file.getOriginalFilename());
        String extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        String endpoint = aliyunOSSProperties.getEndpoint();
        String bucketName = aliyunOSSProperties.getBucketName();
        String url = AliyunOSSUtils.upload(endpoint, bucketName, file.getBytes(), extName);
        return Result.success(url);
    }

}